package com.uchedao.vehicledetection.view.widget.dialog;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.JsonObject;
import com.uchedao.mybaselibrary.http.VolleyUtil;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.activity.MyCarSourceActivity;
import com.uchedao.vehicledetection.config.Api;
import com.uchedao.vehicledetection.network.HttpRequest;
import com.uchedao.vehicledetection.utils.ProgressDialogUtil;
import com.uchedao.vehicledetection.utils.T;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hang on 2015/9/18.
 */
public class ConfirmCheckCarDialog extends CommonDialog implements View.OnClickListener {

    private String id;
    private RequestCallback callback;

    public ConfirmCheckCarDialog(Context context) {
        super(context, R.layout.dlg_confirm_check_car);
    }

    public void setCarId(String id) {
        this.id = id;
    }

    @Override
    public void initDlgView() {
        dlgView.findViewById(R.id.btn_cancel).setOnClickListener(this);
        dlgView.findViewById(R.id.btn_ok).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;

            case R.id.btn_ok:
                requestCheckCar();
                break;
        }
    }

    public void requestCheckCar() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", id);
        Request req = HttpRequest.getRequest(HttpRequest.POST, Api.Action.DO_CHECK_CAR, params,
                new Response.Listener<JsonObject>() {
                    @Override
                    public void onResponse(JsonObject response) {
                        ProgressDialogUtil.dismissProgressDlg();
                        dismiss();
                        if(callback != null)
                            callback.requestCompleted();
                        context.startActivity(new Intent(context, MyCarSourceActivity.class));
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        ProgressDialogUtil.dismissProgressDlg();
                        T.showShort(context, error.getMessage());
                    }
                });
        ProgressDialogUtil.showProgressDlg(context, "");
        VolleyUtil.getInstance(context).getRequestQueue(context).add(req);
    }

    public void setRequestCallback(RequestCallback callback) {
        this.callback = callback;
    }

    public interface RequestCallback {
        public void requestCompleted();
    }
}
